package tool;
import entity.BorrowBook;
import entity.ReaderType;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyTool {
    private static Scanner scanner = new Scanner(System.in);
    /**
     * 输出集合
     * @param arr
     * @param <T>
     */
    public static <T> void printArr(List<T> arr) {
        if(arr!=null) {
            for (int i = 1; i <= arr.size(); i++) {
                System.out.println(i + "-----" + arr.get(i - 1).toString());
            }
        }
    }



    public static ArrayList<BorrowBook>  printArrs(List<BorrowBook> arr) {
        ArrayList<BorrowBook> arrs=new ArrayList<>();
        if(arr!=null) {
            for (int i = 0; i < arr.size(); i++) {
                if(arr.get(i).getReturndate()==null)
                {
                    arrs.add(arr.get(i));
                }
            }
            printArr(arrs);
        }
        return arrs;
    }

    public static <T> void printArr2(List<T> arr) {
        if (arr != null) {
            for (int i = 1; i <= arr.size(); i++) {
                System.out.println(i + "-----" + arr.get(i - 1).toString());
            }
        }
    }
    public static <T> void printOne(T t) {
        if(t!=null)
        System.out.println(1 + "-----" + t.toString());
    }



    /**
     * 获取一个10位的ISBN
     * @return
     * @throws IOException
     */
    public static String getISBN() throws IOException {
        Random r = new Random();
       StringBuilder s= new StringBuilder();
        for (int j = 0; j < 10 ; j++) {
            int i1 = r.nextInt(9);
            s.append(i1);
        }
        return s.toString();
    }

    /**
     * 获取一个正确的日期格式（yyyy—mm-dd）
     * @return
     */
    public static Date getDate() {
        String s =TSUtility.readString(20,"");
        if(s.equals("")){
            return null;
        }
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            date = sdf.parse(s);
        } catch (ParseException e) {
            System.out.println("输入的日期不合法，请重新输入");
            date = getDate();
        }
        return date;
    }

    /**
     * 获取所选中的书本类型在集合中的位置
     * @param arrSize
     * @return
     */
    public static Integer getTypeIndex(int arrSize){
        Integer index =MyTool.readInt();
        if(index==null){
            return index;
        }
        index--;
        if(index<0||index>=arrSize){
            System.out.println("输入有误，请重新输入：");
            index=getTypeIndex(arrSize);
        }
     return index;
    }

    /**
     * 获取一个合法ISBN编号
     * @return
     */
    public static String inputISBN(){
        String ISBN = TSUtility.readString(10,"");
        if(ISBN.equals("")){
            return "";
        }
        if(ISBN.length()!=10){
            System.out.println("输入有误，ISBN为10位");
            ISBN=inputISBN();
        }
    return ISBN;
    }

    /**
     * 获取一个Integer类型的数据，如果输入enter则返回null
     * @return
     */
    public static Integer readInt() {
        int n;
        for (; ; ) {
            String str = TSUtility.readString(6,"");
            if (str.equals("")){
                return null ;
            }
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }

    public static Float readFloat() {
        Float n;
        for (; ; ) {
            String str = TSUtility.readString(5,"");
            if(str.equals("")){
                return null;
            }
            try {
                n = Float.parseFloat(str);
                break;
            } catch (NumberFormatException e) {
                System.out.print("数字输入错误，请重新输入：");
            }
        }
        return n;
    }


    /**
     * 菜单选择
     * @param limit：长度
     * @return
     */
    public static String  selectMenu(int limit){
        String s = scanner.next();
        int i=0;
        try {
            if(s.length()!=1){
                throw new Exception();
            }
             i = Integer.parseInt(s);
            if (i<1||i>limit){
                throw new Exception();
            }
        } catch (Exception e) {
            System.out.println("选择错误，请重新选择：");
            s=selectMenu(limit);
        }
        return s;
    }

    /**
     * 账号密码的录入
     * @return
     */
    public static String getSixString(boolean entry,String DefaultValue){
        String s = TSUtility.readString(20,"");
        if(entry&&s.equals("")){
            return DefaultValue;
        }
        if(s.length()<6){
            System.out.println("至少需要6位");
            s=getSixString(entry,DefaultValue);
        }
        return s;
    }

    public static void main(String[] args) throws Exception {
        Date date=new Date();
        System.out.println(date.toString());
    }
}
